from flask import Blueprint,render_template,g,request
from models.stat.StatDailySite import StatDailySite
from models.stat.StatDailyFood import StatDailyFood
from models.stat.StatDailyMember import StatDailyMember
from Service import Pagination
import datetime
import random
from app import db
route_stat = Blueprint('stat_page',__name__)

@route_stat.route("/index")
def index():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))

    query = StatDailySite.query

    # 分页
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.order_by(StatDailySite.id.desc()).offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    pagination["data_list"] = data_list
    return render_template("stat/index.html",current_user=current_user,pagination=pagination)

@route_stat.route("/food")
def food():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))

    query = StatDailyFood.query

    # 分页
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.order_by(StatDailyFood.id.desc()).offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    pagination["data_list"] = data_list

    return render_template("stat/food.html",current_user=current_user,pagination=pagination)

@route_stat.route("/member")
def member():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))

    query = StatDailyMember.query
    # 分页
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.order_by(StatDailyMember.id.desc()).offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    pagination["data_list"] = data_list

    return render_template("stat/member.html",current_user=current_user,pagination=pagination)

@route_stat.route("/share")
def share():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))

    query = StatDailySite.query
    # 分页
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.order_by(StatDailySite.id.desc()).offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    pagination["data_list"] = data_list
    return render_template("stat/share.html",current_user=current_user,pagination=pagination)
